package com.xgk.boot.module.core.service.channel;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzt.logapi.context.LogRecordContext;
import com.mzt.logapi.service.impl.DiffParseFunction;
import com.mzt.logapi.starter.annotation.LogRecord;
import com.xgk.boot.framework.common.pojo.PageResult;
import com.xgk.boot.framework.common.util.object.BeanUtils;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiPageReqVO;
import com.xgk.boot.module.core.controller.admin.channel.vo.spapi.ApiSaveReqVO;
import com.xgk.boot.module.core.dal.entity.channel.ApiServiceDO;
import com.xgk.boot.module.core.dal.iservice.ServiceProviderAPIService;
import com.xgk.boot.module.core.dal.mapper.channel.ApiServiceMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.xgk.boot.framework.common.constant.ErrorCodeConstants.*;
import static com.xgk.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xgk.boot.module.core.enums.LogRecordConstants.*;

@Service
public class ServiceProviderAPIServiceImpl extends ServiceImpl<ApiServiceMapper, ApiServiceDO> implements ServiceProviderAPIService {

    @Resource
    private ApiServiceMapper apiServiceMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    @LogRecord(type = SYSTEM_API_TYPE, subType = SYSTEM_API_CREATE_TYPE, bizNo = "{{#api.asId}}",
            success = SYSTEM_API_CREATE_SUCCESS)
    public Long createApi(ApiSaveReqVO createReqVO) {
        validateApiDuplicate(createReqVO.getAsName(), createReqVO.getAsCode());

        ApiServiceDO api = BeanUtils.toBean(createReqVO,ApiServiceDO.class);
        apiServiceMapper.insert(api);

        // 3. 记录操作日志上下文
        LogRecordContext.putVariable("api", api);
        return api.getAsId();
    }

    private void validateApiDuplicate(String name, String code) {
        ApiServiceDO api = apiServiceMapper.selectByName(name);
        if (api != null) {
            throw exception(API_EXISTS, name);
        }
        if (!StringUtils.hasText(code)) {
            return;
        }
        api = apiServiceMapper.selectByCode(code);
        if (api != null ) {
            throw exception(API_CODE_DUPLICATE, code);
        }
    }

    @Override
    @LogRecord(type = SYSTEM_API_TYPE, subType = SYSTEM_API_UPDATE_TYPE, bizNo = "{{#updateReqVO.asId}}",
            success = SYSTEM_API_UPDATE_SUCCESS)
    public void updateApi(ApiSaveReqVO updateReqVO) {
        ApiServiceDO api =validateApiForUpdate(updateReqVO.getAsId());
//        validateApiDuplicate(updateReqVO.getAsName(),updateReqVO.getAsCode());
        ApiServiceDO updateObj = BeanUtils.toBean(updateReqVO, ApiServiceDO.class);
        updateObj.setAsCode(null);
        apiServiceMapper.updateById(updateObj);

        // 3. 记录操作日志上下文
        LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(api, ApiSaveReqVO.class));
        LogRecordContext.putVariable("api", api);
    }
    ApiServiceDO validateApiForUpdate(Long id) {
        ApiServiceDO api = apiServiceMapper.selectById(id);
        if (api == null) {
            throw exception(API_NOT_EXISTS);
        }
       
        return api;
    }
    @Override
    public PageResult<ApiServiceDO> getApiPage(ApiPageReqVO pageReqVO) {
        PageResult<ApiServiceDO> result = apiServiceMapper.selectPage(pageReqVO);
        result.setPageParam(pageReqVO);
        return result;
    }

    @Override
    public void activeApi(Long asId, Integer asStatus) {
        ApiServiceDO api =validateApiForUpdate(asId);
        if(asStatus==STATUS_INACTIVE){
            api.setAsStatus(STATUS_ACTIVE);
        }else{
            api.setAsStatus(STATUS_INACTIVE);
        }
        apiServiceMapper.updateById(api);
    }
    public static int STATUS_ACTIVE=1;
    public static int STATUS_INACTIVE=0;
    @Override
    public List<ApiServiceDO> getApiSelectList() {
        List<ApiServiceDO> all = apiServiceMapper.selectList(ApiServiceDO::getAsStatus, STATUS_ACTIVE);
        return all;
    }
}
